﻿using Oplaco.Samples.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data.Entity;
using System.Threading.Tasks;

namespace Oplaco.Samples.Repository
{
    public class ArticleRepository
    {
        private OplacoSampleDatabaseEntities dbEntity;

        #region Syncrhonous methods
        public void Add(Article source)
        {
            oplArticle target = new oplArticle { Id = source.Id, LastModified = source.LastModified, Title = source.Title };
            using (dbEntity = new OplacoSampleDatabaseEntities()) {
                dbEntity.oplArticles.Add(target);
                dbEntity.SaveChanges();
            }
        }

        public void AddImage(FileItem source)
        {
            oplArticleImage target = new oplArticleImage
            {
                Id = source.Id,
                Alias = source.Alias,
                ArticleId = source.ArticleId,
                Url = source.Url,
                LastModified = DateTime.Now,
                ParentId = source.ParentId,
                ImageType = (Int32)source.FileType
            };
            using (dbEntity = new OplacoSampleDatabaseEntities())
            {
                dbEntity.oplArticleImages.Add(target);
                dbEntity.SaveChanges();
            }
        }

        public Article GetById(Int32 id) {
            Article result = null;
            using (dbEntity = new OplacoSampleDatabaseEntities())
            {
                var query = from a in dbEntity.oplArticles
                            where a.Id.Equals(id)
                            select new Article
                            {
                                Id = a.Id,
                                Title = a.Title,
                                LastModified = a.LastModified,
                                Images = new List<FileItem>(
                                    from i in dbEntity.oplArticleImages
                                    where i.ArticleId.Equals(a.Id)
                                    select new FileItem
                                    {
                                        Id = i.Id,
                                        Alias = i.Alias,
                                        ArticleId = a.Id,
                                        Url = i.Url,
                                        LastModified = i.LastModified
                                    }
                                )
                            };
                if (query.Count() > 0) result = query.First();
            }

            return result;
        }

        public FileItem GetImageById(Int32 id)
        {
            FileItem result = null;
            using (dbEntity = new OplacoSampleDatabaseEntities())
            {
                var query = from i in dbEntity.oplArticleImages
                            where i.Id.Equals(id)
                            select new FileItem
                            {
                                Id = i.Id,
                                Alias = i.Alias,
                                ArticleId = i.ArticleId,
                                Url = i.Url,
                                LastModified = i.LastModified
                            };                               
                            
                if (query.Count() > 0) result = query.First();
            }

            return result;
        }

        public void UpdateImage(FileItem source)
        {
            
            using (dbEntity = new OplacoSampleDatabaseEntities())
            {
                var query = from i in dbEntity.oplArticleImages where i.Id.Equals(source.Id) select i;
                if (query.Count() > 0) {
                    oplArticleImage target = query.First();
                    target.Alias = source.Alias;
                    target.LastModified = source.LastModified;
                    target.Url = source.Url;
                    target.ParentId = source.ParentId;
                    target.ImageType = (Int32)source.FileType;
                    dbEntity.SaveChanges();
                }
                
            }
        }
        #endregion

        #region Asynchronous

        public async Task AddImageAsync(FileItem source)
        {
            oplArticleImage target = new oplArticleImage { 
                Id = source.Id, 
                Alias = source.Alias, 
                ArticleId = source.ArticleId, 
                Url = source.Url,
                LastModified = DateTime.Now,
                ParentId = source.ParentId,
                ImageType = (Int32)source.FileType
            };
            using (dbEntity = new OplacoSampleDatabaseEntities())
            {
                dbEntity.oplArticleImages.Add(target);
                await dbEntity.SaveChangesAsync();
            }
        }

        public async Task<FileItem> GetImageByIdAsync(Int32 id)
        {
            FileItem result = null;
            using (dbEntity = new OplacoSampleDatabaseEntities())
            {
                var query = await (from i in dbEntity.oplArticleImages
                                   where i.Id.Equals(id)
                                   select i).ToListAsync();

                result = (from img in query select new FileItem
                            {
                                Id = img.Id,
                                Alias = img.Alias,
                                ArticleId = img.ArticleId,
                                Url = img.Url,
                                LastModified = img.LastModified
                            }).FirstOrDefault();
            }

            return result;
        }

        public async Task UpdateImageAsync(FileItem source)
        {

            using (dbEntity = new OplacoSampleDatabaseEntities())
            {
                var query = from i in dbEntity.oplArticleImages where i.Id.Equals(source.Id) select i;
                if (query.Count() > 0)
                {
                    oplArticleImage target = query.First();
                    target.Alias = source.Alias;
                    target.LastModified = source.LastModified;
                    target.Url = source.Url;
                    
                    await dbEntity.SaveChangesAsync();
                }

            }
        }

        public async Task AddArticleAsync(Article source)
        {
            
            oplArticle target = new oplArticle
            {
                Id = Guid.NewGuid(),
                Title = source.Title,
                LastModified = DateTime.Now,
                oplArticleImages = (from i in source.Images select new oplArticleImage { 
                    Alias = i.Alias,
                    LastModified = DateTime.Now,
                    Url = i.Url,
                    Id = Guid.NewGuid(), 
                }).ToList(),
            };
            using (dbEntity = new OplacoSampleDatabaseEntities())
            {
                dbEntity.oplArticles.Add(target);
                await dbEntity.SaveChangesAsync();
            }
        }

        #endregion

        
        private async Task<Guid> GetArticleIdAsync()
        {
            Guid result = Guid.Empty;
            using (dbEntity = new OplacoSampleDatabaseEntities())
            {
                var query = await (from a in dbEntity.oplArticles
                                   orderby a.Id descending
                                   select a.Id).ToListAsync();
                result = query.First();
            }
            return result;
        }
    }
}
